"""
自定义是否登录逻辑
"""

from django import http
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views import View


class LoginRequiredJsonMixin(LoginRequiredMixin, View):
    def handle_no_permission(self):
        # print('21')
        return http.JsonResponse({'code': 400, 'errmsg': '用户未登录'})


# 使用装饰器验证是否登录
def login_required(view):
    def wrapper(request, *args, **kwargs):
        if not request.user.is_authenticated:
            return http.JsonResponse({'code': 400, 'errmsg': '用户未登录'})
        return view(request, *args, **kwargs)
    return wrapper


